﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight;
using System.Json;
using WorkSpace.UI.WorkSpaceRef;
using WorkSpace.UI.Controls.Windows;
using WorkSpace.UI.Controls;
using System.Collections.ObjectModel;

namespace WorkSpace.UI.Windows
{
    public partial class PersonalCategoryWindow : SlWindow
    {
        private PersonalCategoryPart _categoryPart;
        private PackagePart _packagePart;

        private List<SelectItem<Link>> _items;
        private Category _category;
        private PersonalPackage _curPackage;
        private string _account;

        private bool _isModified = false;

        public PersonalCategoryWindow(PersonalCategoryPart categoryPart, PackagePart packagePart, string account)
        {
            InitializeComponent();

            _categoryPart = categoryPart;
            _packagePart = packagePart;

            _category = _categoryPart.Category;
            _curPackage = _packagePart.Package;

            _ctlEditPackage.Text = _curPackage.Caption;

            _account = account;

            this.Loaded += new RoutedEventHandler(CategoryListWindow_Loaded);
        }

        void CategoryListWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDatas();
        }

        private void LoadDatas()
        {
            LoadPackages();
            LoadLinks();
        }

        #region 加载连接数据
        private void LoadLinks()
        {
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetAllLinksCompleted += new EventHandler<GetAllLinksCompletedEventArgs>(client_GetAllLinksCompleted);
            client.GetAllLinksAsync(_category.Id, _account);
        }

        void client_GetAllLinksCompleted(object sender, GetAllLinksCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _items = new List<SelectItem<Link>>();

                foreach (Link item in e.Result.Data)
                {
                    _items.Add(new SelectItem<Link>(item));
                }

                WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
                client.GetPersonalLinksCompleted += new EventHandler<GetPersonalLinksCompletedEventArgs>(client_GetPersonalLinksCompleted);
                client.GetPersonalLinksAsync(_curPackage.Id);
            }
        }

        void client_GetPersonalLinksCompleted(object sender, GetPersonalLinksCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                foreach (SelectItem<Link> item in _items)
                {
                    foreach (PersonalLink pLink in e.Result.Data)
                    {
                        if (pLink.LinkId == item.Data.Id)
                        {
                            item.IsSelected = true;
                            break;
                        }
                    }
                }

                _ctlGridLink.ItemsSource = _items;
            }
        }
        #endregion

        #region 加载页面数据
        public void PackagedSourceChanged(ObservableCollection<PersonalPackage> packages, PackagePart curPackage)
        {
            _packagePart = curPackage;
            _curPackage = _packagePart.Package;

            _ctlEditPackage.Text = _curPackage.Caption;

            LoadLinks();

            _ctlGridPackage.ItemsSource = packages;
        }

        private void LoadPackages()
        {
            _ctlGridPackage.ItemsSource = _categoryPart.Packages;
        }
        #endregion

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (_isModified)
            {
                this.DialogResult = true;
            }
            else
            {
                this.DialogResult = false;
            }
        }

        #region 连接操作
        private void _btnEnable_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton button = sender as HyperlinkButton;
            SelectItem<Link> item = button.DataContext as SelectItem<Link>;

            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.AddPersonalLinkCompleted += new EventHandler<AddPersonalLinkCompletedEventArgs>(client_AddPersonalLinkCompleted);
            client.AddPersonalLinkAsync(_curPackage.Id, item.Data.Id);
        }

        void client_AddPersonalLinkCompleted(object sender, AddPersonalLinkCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _packagePart.LoadDatas();
                LoadLinks();
            }
        }

        private void _btnDisable_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton button = sender as HyperlinkButton;
            SelectItem<Link> item = button.DataContext as SelectItem<Link>;


            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.RemovePersonalLinkCompleted += new EventHandler<RemovePersonalLinkCompletedEventArgs>(client_RemovePersonalLinkCompleted);
            client.RemovePersonalLinkAsync(_curPackage.Id, item.Data.Id);
        }

        void client_RemovePersonalLinkCompleted(object sender, RemovePersonalLinkCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                _packagePart.LoadDatas();
                LoadLinks();
            }
        }
        #endregion

        #region 页面操作
        private void _btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (_categoryPart.Packages.Count == 1)
            {
                MessageBox.Show("该类别只有一个显示页，无法删除!");
                return;
            }
            FrameworkElement element = sender as FrameworkElement;
            
            PersonalPackage package =  element.DataContext as PersonalPackage;

            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.RemovePersonalPackageCompleted += new EventHandler<RemovePersonalPackageCompletedEventArgs>(client_RemovePersonalPackageCompleted);
            client.RemovePersonalPackageAsync(package.Id, package);
        }

        void client_RemovePersonalPackageCompleted(object sender, RemovePersonalPackageCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                PersonalPackage package = e.UserState as PersonalPackage;
                if (_curPackage.Id == package.Id)
                {
                    for (int index = 0; index < _categoryPart.Packages.Count; index++)
                    {
                        if (_categoryPart.Packages[index].Id == package.Id )
                        {
                            if (index > 0)
                            {
                                _categoryPart.LoadDatas(_categoryPart.Packages[index - 1].Id);
                                return;
                            }
                            else if (index < _categoryPart.Packages.Count - 1)
                            {
                                _categoryPart.LoadDatas(_categoryPart.Packages[index + 1].Id);
                                return;
                            }
                            else
                            {
                                _categoryPart.LoadDatas();
                            }
                        }
                    }
                }
                _categoryPart.LoadDatas();
            }
        }

        #region 创建新页
        void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            PersonalPackage package = new PersonalPackage();
            package.Id = Guid.NewGuid();
            package.Caption = "新显示页";
            package.Account = _account;
            package.CategoryId = _category.Id;

            InputPackageWindow packageWindow = new InputPackageWindow(package);
            packageWindow.Closed += new EventHandler(packageWindow_Closed);
            packageWindow.Show();
        }

        void packageWindow_Closed(object sender, EventArgs e)
        {
            InputPackageWindow packageWindow = sender as InputPackageWindow;
            if (packageWindow.DialogResult == true)
            {
                WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
                client.SavePersonalPackageCompleted += new EventHandler<WorkSpaceRef.SavePersonalPackageCompletedEventArgs>(client_SavePersonalPackageCompleted);
                client.SavePersonalPackageAsync(packageWindow.Package, packageWindow.Package);
            }
        }

        void client_SavePersonalPackageCompleted(object sender, WorkSpaceRef.SavePersonalPackageCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                PersonalPackage package = e.UserState as PersonalPackage;
                _categoryPart.LoadDatas(package.Id);
            }
        }
        #endregion
        #endregion
    }
}

